package com.yazi.mxz.common.db.dao.impl;


import java.util.HashMap;
import java.util.Map;

import org.hibernate.Criteria;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;
import org.hibernate.criterion.Subqueries;
import org.springframework.stereotype.Repository;

import com.yazi.mxz.common.db.dao.GoodsDao;
import com.yazi.mxz.common.entity.beans.Category;
import com.yazi.mxz.common.entity.beans.Company;
import com.yazi.mxz.common.entity.beans.Goods;
import com.yazi.mxz.common.entity.beans.GoodsClassify;
import com.yazi.mxz.common.entity.beans.StoreGoods;
import com.myframework.smhj.hibernate.Searching;
import com.myframework.smhj.hibernate.Page;
import com.myframework.smhj.hibernate.dao.BasicDao;

@Repository("goodsDao")
public class GoodsDaoImpl extends BasicDao<Goods> implements GoodsDao {

	
	public GoodsDaoImpl() {
		super(Goods.class);
		
	}

	@Override
	public Criteria getExample(Goods goods, String searchWord) {
		Criteria c = super.getExample(goods, searchWord);
		
		// 关联对象条件
		if (null != goods) {
			// 企业
			Company company = goods.getCompany();
			if (null != company) {
				// 企业ID
				if (null != company.getCompanyId()) {
					c.add(Restrictions.eq("company.companyId", company.getCompanyId()));
				} else {
					// ...企业其它条件
				}
			}
			// 系统分类
			Category category = goods.getCategory();
			if (null != category) {
				// 分类ID
				if (null != category.getCatId()) {
					DetachedCriteria dc = DetachedCriteria.forClass(Category.class);
					dc.add(Restrictions.sqlRestriction("1=1 START WITH cat_id=" + category.getCatId() + " CONNECT BY PRIOR cat_id= parent_id "));
					dc.setProjection(Projections.property("catId"));
					c.add(Subqueries.propertyIn("category.catId", dc));
				} else {
					// ...分类其它条件
				}
			}			
			// (企业)自定义分类
			GoodsClassify classify = goods.getGoodsClassify();
			if (null != classify) {
				// 分类ID
				if (null != classify.getClassifyId()) {
					DetachedCriteria dc = DetachedCriteria.forClass(GoodsClassify.class);
					dc.add(Restrictions.sqlRestriction("1=1 START WITH classify_id=" + classify.getClassifyId() + " CONNECT BY PRIOR classify_id= parent_id "));
					dc.setProjection(Projections.property("classifyId"));
					c.add(Subqueries.propertyIn("goodsClassify.classifyId", dc));
				} else {
					// ...分类的其它条件
				}
			}
			// ...其它关联对象条件
		}
		
		// 关键字条件
		if (null != searchWord && !searchWord.isEmpty()) {
			// 关键字可查商品名称,商品编号
			String likeWord = "%" + searchWord + "%";
			c.add(Restrictions.or(Restrictions.like("name", likeWord),
							Restrictions.like("barCode", likeWord)));
		}
		
		return c;
	}

	@Override
	public Page<Goods> listForAddByStoreId(long storeId, Goods goods, Searching si) {

		Criteria c = getExample(goods, si.getSearchWord());
		
		DetachedCriteria dc = DetachedCriteria.forClass(StoreGoods.class, "sg");
		dc.add(Restrictions.eq("sg.store.storeId", storeId));
		dc.add(Property.forName("sg.goods.goodsId").eqProperty(c.getAlias() + ".goodsId"));

		c.add(Subqueries.notExists(dc.setProjection(Projections.property("sg.storeGoodsId"))));
		c.setCacheable(false);
		return list(c, si);
	}

	@Override
	public void move(long fromClassifyId, long toClassifyId) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("from_id", fromClassifyId);
		param.put("to_id", toClassifyId);
		executeUpdate("updateClassifyIdSql", param);
	}

	@Override
	public void moveByCatId(long fromCatId, long toCatId) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("from_id", fromCatId);
		param.put("to_id", toCatId);
		executeUpdate("updateCatIdSql", param);
	}
	
	
}
